package com.hlk.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.hlk.article.mapper.CommentsMapper;
import com.hlk.article.service.ArticlePortalService;
import com.hlk.article.service.CommentPortalService;
import com.hlk.pojo.Comments;
import com.hlk.pojo.vo.ArticleDetailVO;
import com.hlk.pojo.vo.CommentsVO;
import com.hlk.utils.PageResult;
import com.hlk.utils.PageUtils;
import com.hlk.utils.redis.RedisKeys;
import com.hlk.utils.redis.RedisUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hlk
 * @version 1.0
 * @date 2021-01-08 15:13
 */
@Service
public class CommentPortalServiceImpl implements CommentPortalService {
    @Autowired
    private ArticlePortalService articlePortalService;
    @Autowired
    private Sid sid;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private CommentsMapper commentsMapper;

    @Override
    public void createComment(String articleId, String fatherCommentId, String content, String userId, String nickname, String face) {
        String commentId = sid.nextShort();

        ArticleDetailVO article
                = articlePortalService.queryDetail(articleId);

        Comments comments = new Comments();
        comments.setId(commentId);

        comments.setWriterId(article.getPublishUserId());
        comments.setArticleTitle(article.getTitle());
        comments.setArticleCover(article.getCover());
        comments.setArticleId(articleId);

        comments.setFatherId(fatherCommentId);
        comments.setCommentUserId(userId);
        comments.setCommentUserNickname(nickname);
        comments.setCommentUserFace(face);

        comments.setContent(content);
        comments.setCreateTime(new Date());

        commentsMapper.insert(comments);

        // 评论数累加
        redisUtils.increment(RedisKeys.getArticleCommentCounts(articleId), 1);
    }

    @Override
    public PageResult queryArticleComments(String articleId, Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("articleId", articleId);

        PageHelper.startPage(page, pageSize);
        List<CommentsVO> list = commentsMapper.queryArticleCommentList(map);
        return PageUtils.getResult(page, list);
    }

    @Override
    public PageResult queryWriterCommentsMng(String writerId, Integer page, Integer pageSize) {
        Comments comment = new Comments();
        comment.setWriterId(writerId);

        PageHelper.startPage(page, pageSize);
        List<Comments> list = commentsMapper.select(comment);
        return PageUtils.getResult(page, list);
    }

    @Override
    public void deleteComment(String writerId, String commentId, String articleId) {
        Comments comment = new Comments();
        comment.setId(commentId);
        comment.setWriterId(writerId);

        commentsMapper.delete(comment);
        redisUtils.decrement(RedisKeys.getArticleCommentCounts(articleId), 1);
    }
}
